<%@ page contentType="application/json;charset=UTF-8" language="java" %><%--
--%><%@ page import="java.util.Collection" %><%--
--%><%@ page import="o.json.JSONFactory" %><%--
--%><%@ page import="o.json.JSONValue" %><%--
--%><%@ page import="o.json.JSONWriter" %><%--
--%><%@ page import="o.json.JSONWriter2" %><%--
--%><%@ page import="com.google.appengine.api.users.User" %><%--
--%><%@ page import="com.google.appengine.api.users.UserService" %><%--
--%><%@ page import="com.google.appengine.api.users.UserServiceFactory" %><%--
--%><%@ page import="wwiii.AreaMap" %><%--
--%><%@ page import="wwiii.MapGenerator" %><%--
--%><%@ page import="wwiii.Game" %><%--
--%><%@ page import="wwiii.GameManager" %><%--
--%><%!

	public int parseInt(String value, int def) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			return def;
		}
	}
	public String notNull(String value, String def) {
		if (value != null && value != "") {
			return value;
		}

		return def;
	}

%><%

	final UserService us = UserServiceFactory.getUserService();
	if (!us.isUserLoggedIn()) {
		response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		return;
	}
	final User user = us.getCurrentUser();

	String id = request.getParameter("id");
	if (id == null || id == "") {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST, "id is not valid '"+ id +"'");
		return;
	}

	final GameManager gm = new GameManager();
	if (gm.gameExists(id)) {
		gm.removeGame(id);
	}

	final Collection<Game> games = gm.loadGames();

	final JSONWriter writer = new JSONWriter2();
	final JSONFactory factory = new JSONFactory();
	writer.write(factory.create(games));

%><%= writer.toString() %>
